﻿using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens;
using Microsoft.IdentityModel.Claims;

namespace Microsoft.IdentityModel.OAuth
{
    public class SimpleWebToken : SecurityToken
    {
        private SWTParser parser;

        /// <summary>
        /// Creates a new instance of SimpleWebToken and optionally parses it
        /// </summary>
        /// <param name="rawToken">URL decoded SWT</param>
        /// <param name="autoParse">true if parsing is required, otherwise false.</param>
        public SimpleWebToken(string rawToken, bool autoParse)
        {
            this.RawToken = rawToken;

            if (autoParse)
                parser = new SWTParser(this.RawToken);
        }
        /// <summary>
        /// Creates a new instance of SimpleWebToken without parsing the raw token.
        /// </summary>
        /// <param name="rawToken">URL decoded SWT</param>
        public SimpleWebToken(string rawToken):this(rawToken, false){}

        public string RawToken { get; private set; }

        public override string Id
        {
            get { throw new NotImplementedException(); }
        }

        public override System.Collections.ObjectModel.ReadOnlyCollection<SecurityKey> SecurityKeys
        {
            get { throw new NotImplementedException(); }
        }

        public override DateTime ValidFrom
        {
            get { throw new NotImplementedException(); }
        }

        public override DateTime ValidTo
        {
            get { throw new NotImplementedException(); }
        }

        public string Issuer 
        { 
            get 
            {
                EnsureParser();
                return parser.Issuer; 
            } 
        }

        public string Audience 
        { 
            get 
            {
                EnsureParser();
                return parser.Audience; 
            } 
        }

        public List<Claim> Claims
        {
            get
            {
                EnsureParser();
                return parser.Claims;
            }
        }

        void EnsureParser()
        {
            if (parser == null)
                parser = new SWTParser(this.RawToken);
        }
    }
}
